//
//  BGAdViewController.m
//  BluetoothCameraHead
//
//  Created by wang on 16/11/1.
//  Copyright © 2016年 BG. All rights reserved.
//

#import "BGAdViewController.h"
#import "BGNewFeaturesViewController.h"
#import "BGSaveTool.h"


#define BGLoadImageKey @"BGLoadImage"
#define BGVersionKey @"CFBundleShortVersionString"





@interface BGAdViewController ()
@property (nonatomic,strong) UIImage *launchImage;
@end

@implementation BGAdViewController

- (UIImage *)launchImage
{
    if (_launchImage == nil) {
        NSDictionary *dict = @{@"320x480" : @"LaunchImage-700", @"320x568" : @"LaunchImage-700-568h", @"375x667" : @"LaunchImage-800-667h", @"414x736" : @"LaunchImage-800-Portrait-736h"};
        NSString *key = [NSString stringWithFormat:@"%dx%d", (int)[UIScreen mainScreen].bounds.size.width, (int)[UIScreen mainScreen].bounds.size.height];
        _launchImage = [UIImage imageNamed:dict[key]];
    }
    return _launchImage;
}



- (void)viewDidLoad {
    [super viewDidLoad];
   
    //这种方式会自动拉伸图片，而且没有额外内存占用。
    self.view.layer.contents = (id) self.launchImage.CGImage;
    [UIApplication sharedApplication].statusBarHidden = YES;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //判断第一次进入进入简介，其他就进入主页面
        [self chooseRootViewController];
    });
}

- (void)chooseRootViewController
{
    //3. 2s后跳转到下一个主界面
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // 如何知道第一次使用这个版本？比较上次的使用情况
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if ([self isOldVersion]) {
            // 当前版本号 == 上次使用的版本：显示ViewController
            [UIApplication sharedApplication].statusBarHidden = NO;
            //            window.rootViewController = [[BGMainViewController alloc] init];
            // 发出切换根视图的通知
            [[NSNotificationCenter defaultCenter] postNotificationName:BGSwitchRootViewControllerNotification object:nil];
        } else { // 当前版本号 != 上次使用的版本：显示版本新特性
            window.rootViewController = [[BGNewFeaturesViewController alloc] init];
            
            // 获得当前打开软件的版本号
            NSString *currentVersion = [NSBundle mainBundle].infoDictionary[BGVersionKey];
            // 存储这次使用的软件版本
            [BGSaveTool setObject:currentVersion forKey:BGVersionKey];
        }
    });
}

- (BOOL)isOldVersion
{
    // 从沙盒中取出上次存储的软件版本号(取出用户上次的使用记录)
    NSString *lastVersion = [BGSaveTool objectForKey:BGVersionKey];
    // 获得当前打开软件的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[BGVersionKey];
    
    return [currentVersion isEqualToString:lastVersion];
}



@end
